package com.dianping.cat.home.dependency.graph.transform;

import com.dianping.cat.home.dependency.graph.IVisitor;
import com.dianping.cat.home.dependency.graph.entity.TopologyEdge;
import com.dianping.cat.home.dependency.graph.entity.TopologyGraph;
import com.dianping.cat.home.dependency.graph.entity.TopologyNode;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;



public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

  public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
    this.m_out = new DataOutputStream(out);
    this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(TopologyGraph topologyGraph) {
    ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

    build(topologyGraph, out);
    return out.toByteArray();
  }


  public static void build(TopologyGraph topologyGraph, OutputStream out) { topologyGraph.accept(new DefaultNativeBuilder(out)); }



  public void visitTopologyEdge(TopologyEdge topologyEdge) {
/*  41 */     if (topologyEdge.getKey() != null) {
/*  42 */       writeTag(1, 1);
/*  43 */       writeString(topologyEdge.getKey());
    }

/*  46 */     if (topologyEdge.getType() != null) {
/*  47 */       writeTag(2, 1);
/*  48 */       writeString(topologyEdge.getType());
    }

/*  51 */     if (topologyEdge.getTarget() != null) {
/*  52 */       writeTag(3, 1);
/*  53 */       writeString(topologyEdge.getTarget());
    }

/*  56 */     writeTag(4, 0);
/*  57 */     writeBoolean(topologyEdge.getOpposite());

/*  59 */     writeTag(5, 0);
/*  60 */     writeInt(topologyEdge.getWeight());

/*  62 */     writeTag(6, 0);
/*  63 */     writeInt(topologyEdge.getStatus());

/*  65 */     if (topologyEdge.getDes() != null) {
/*  66 */       writeTag(7, 1);
/*  67 */       writeString(topologyEdge.getDes());
    }

/*  70 */     if (topologyEdge.getLink() != null) {
/*  71 */       writeTag(8, 1);
/*  72 */       writeString(topologyEdge.getLink());
    }

/*  75 */     if (topologyEdge.getSelf() != null) {
/*  76 */       writeTag(9, 1);
/*  77 */       writeString(topologyEdge.getSelf());
    }

/*  80 */     writeTag(10, 0);
/*  81 */     writeBoolean(topologyEdge.getDashed());

/*  83 */     writeTag(63, 3);
  }


  public void visitTopologyGraph(TopologyGraph topologyGraph) {
/*  88 */     writeTag(63, 0);

/*  90 */     if (topologyGraph.getId() != null) {
/*  91 */       writeTag(1, 1);
/*  92 */       writeString(topologyGraph.getId());
    }

    if (topologyGraph.getType() != null) {
/*  96 */       writeTag(2, 1);
/*  97 */       writeString(topologyGraph.getType());
    }

/* 100 */     writeTag(3, 0);
/* 101 */     writeInt(topologyGraph.getStatus());

/* 103 */     if (topologyGraph.getDes() != null) {
/* 104 */       writeTag(4, 1);
/* 105 */       writeString(topologyGraph.getDes());
    }

/* 108 */     if (!topologyGraph.getNodes().isEmpty()) {
/* 109 */       writeTag(33, 2);
/* 110 */       writeInt(topologyGraph.getNodes().size());

/* 112 */       for (TopologyNode topologyNode : topologyGraph.getNodes().values()) {
/* 113 */         topologyNode.accept(this.m_visitor);
      }
    }

/* 117 */     if (!topologyGraph.getEdges().isEmpty()) {
/* 118 */       writeTag(34, 2);
/* 119 */       writeInt(topologyGraph.getEdges().size());

/* 121 */       for (TopologyEdge topologyEdge : topologyGraph.getEdges().values()) {
/* 122 */         topologyEdge.accept(this.m_visitor);
      }
    }

/* 126 */     writeTag(63, 3);
  }


  public void visitTopologyNode(TopologyNode topologyNode) {
/* 131 */     if (topologyNode.getId() != null) {
/* 132 */       writeTag(1, 1);
/* 133 */       writeString(topologyNode.getId());
    }

/* 136 */     if (topologyNode.getType() != null) {
/* 137 */       writeTag(2, 1);
/* 138 */       writeString(topologyNode.getType());
    }

/* 141 */     writeTag(3, 0);
/* 142 */     writeInt(topologyNode.getStatus());

/* 144 */     if (topologyNode.getDes() != null) {
/* 145 */       writeTag(4, 1);
/* 146 */       writeString(topologyNode.getDes());
    }

/* 149 */     if (topologyNode.getLink() != null) {
/* 150 */       writeTag(5, 1);
/* 151 */       writeString(topologyNode.getLink());
    }

/* 154 */     writeTag(6, 0);
/* 155 */     writeInt(topologyNode.getWeight());

/* 157 */     writeTag(63, 3);
  }

  private void writeBoolean(boolean value) {
    try {
/* 162 */       this.m_out.writeByte(value ? 1 : 0);
/* 163 */     } catch (IOException e) {
/* 164 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 170 */       writeVarint(value & 0xFFFFFFFFL);
/* 171 */     } catch (IOException e) {
/* 172 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 178 */       this.m_out.writeUTF(value);
/* 179 */     } catch (IOException e) {
/* 180 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 186 */       this.m_out.writeByte((field << 2) + type);
/* 187 */     } catch (IOException e) {
/* 188 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 194 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 195 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 198 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 199 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/dependency/graph/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */